package alibaba.client.utils;

import alibaba.common.utils.CommonUtils;

public class Utils {

    public static String convertToUnixPath(String path) {
        if (path == null || path.indexOf(CommonUtils.WINDOWS_SEPARATOR) == -1) {
            return removeDriveFromPath(path);
        }
        String separatorFixedPath = path.replace(CommonUtils.WINDOWS_SEPARATOR,
        		CommonUtils.UNIX_SEPARATOR);

        if (separatorFixedPath.length() <= 1) {
            return CommonUtils.makeStartWithUnixSeparator(separatorFixedPath);
        }
        
        String seperatorPrepended = removeDriveFromPath(separatorFixedPath);
        seperatorPrepended = CommonUtils.makeStartWithUnixSeparator(seperatorPrepended);
        
        return seperatorPrepended;
    }

    private static String removeDriveFromPath(String path) {
        if (path == null || path.length() <= 1) {
            return path;
        }
        
        return path.charAt(1) == ':' ? path.substring(2) : path;
    }
    
}
